package phone.qyg.com.qyg.activity.baidu;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.app.adapter.recycler.CommonAdapter;
import com.app.adapter.recycler.base.ViewHolder;
import com.app.application.QXApplication;
import com.app.base.tool.BaseActivity;
import com.app.utils.CommonUtil;
import com.app.utils.SystemPrintl;
import com.app.view.Panel;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.search.route.BikingRouteResult;
import com.baidu.mapapi.search.route.DrivingRouteResult;
import com.baidu.mapapi.search.route.IndoorRouteResult;
import com.baidu.mapapi.search.route.MassTransitRouteLine;
import com.baidu.mapapi.search.route.MassTransitRoutePlanOption;
import com.baidu.mapapi.search.route.MassTransitRouteResult;
import com.baidu.mapapi.search.route.OnGetRoutePlanResultListener;
import com.baidu.mapapi.search.route.RoutePlanSearch;
import com.baidu.mapapi.search.route.TransitRouteResult;
import com.baidu.mapapi.search.route.WalkingRouteResult;
import com.mapapi.overlayutil.DrivingRouteOverlay;
import com.mapapi.overlayutil.MassTransitRouteOverlay;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;

import java.util.List;

import phone.qyg.com.qyg.R;
import phone.qyg.com.qyg.model.baidu.LineInfo;


/**
 * import com.baidu.mapapi.search.route.DrivingRouteLine;
 * import com.baidu.mapapi.search.route.MassTransitRouteLine;
 */

/**
 * 作者：lblbh on 2017/6/7 15:44
 * 邮箱：lanbuhan@163.com
 * 功能：线路展示
 */
@ContentView(R.layout.line)
public class LineActivity extends BaseActivity {

    @ViewInject(R.id.back)
    TextView back;

    @ViewInject(R.id.bmapView)
    MapView bmapView;

    @ViewInject(R.id.line_name)
    TextView line_name;

    @ViewInject(R.id.user_name)
    TextView item_time;

    @ViewInject(R.id.item_distance)
    TextView item_distance;

    @ViewInject(R.id.item_walk)
    TextView item_walk;

    @ViewInject(R.id.line_state)
    TextView line_state;

    @ViewInject(R.id.transit_linear)
    LinearLayout transit_linear;

    @ViewInject(R.id.linear)
    LinearLayout linear;

    @ViewInject(R.id.panel)
    Panel panel;

    @ViewInject(R.id.recycler)
    RecyclerView recycler;

    RoutePlanSearch mSearch;

    BaiduMap mBaiduMap;

    LineInfo info;

    int item;

    @Override
    public void initSucessView() {

        Object obj = getTransModels();
        if (obj instanceof LineInfo) {
            info = (LineInfo) obj;
        }
    }
//class com.baidu.mapapi.search.route.DrivingRouteLine
    // com.baidu.mapapi.search.core.TransitBaseInfo cannot be cast to com.baidu.mapapi.search.core.b

    @Override
    public void initData(Bundle savedInstanceState) {

        initBMapView();

        setDrawLine();

        setData();
    }

    private void setData() {
        if (info == null) return;
        int time = info.duration;
        int distance = info.distance;

        //时间设置
        if (time / 3600 == 0) {
            item_time.setText(time / 60 + "分钟");
        } else {
            item_time.setText(time / 3600 + "小时" + (time % 3600) / 60 + "分钟");
        }

        //设置距离
        item_distance.setText((float) (Math.round(distance * 10)) / 10000 + "公里");

        int type = info.type;

        if (type == 1) {
            //1-驾车
            transit_linear.setVisibility(View.GONE);

            line_name.setText(info.title);
//            isRequestList(info.lineDetail)?info.lineDetail.get(0):"驾车行驶"
        } else {
            //0-公交
            int walk = info.walk;
            line_name.setText(info.line_name);
            transit_linear.setVisibility((walk > 0) ? View.VISIBLE : View.GONE);
            item_walk.setText("步行" + walk + "米");

        }

        List<String> lineDetail = info.lineDetail;
        if (!isRequestList(lineDetail))
            return;

        CommonAdapter adapter = new CommonAdapter<String>(QXApplication.getContext(), R.layout.item_line_details, lineDetail) {
            @Override
            protected void convert(ViewHolder holder, final String title, final int position) {
                holder.setTextHTML(R.id.item_title, title);
            }
        };
        recycler.setAdapter(adapter);
    }

    private void setDrawLine() {
        if (info == null) return;
//        public int type;          //0-公交 1-驾车
        if (info.type == 0) {
//            MassTransitRouteOverlay overlay = new MyMassTransitRouteOverlay(mBaiduMap);
//            mBaiduMap.setOnMarkerClickListener(overlay);
//            overlay.setData(info.massTransitRouteLine);
//            overlay.addToMap();
//            overlay.zoomToSpan();
//            MapStatusUpdate msu = MapStatusUpdateFactory.zoomTo(17.3f);
//            mBaiduMap.setMapStatus(msu);
            item = info.item;
            mSearch = RoutePlanSearch.newInstance();
            mSearch.setOnGetRoutePlanResultListener(routeListener);
            mSearch.masstransitSearch(new MassTransitRoutePlanOption().from(info.tranStarNode).to(info.tranEndNode));
        } else {
            DrivingRouteOverlay overlay = new MyDrivingRouteOverlay(mBaiduMap);
            mBaiduMap.setOnMarkerClickListener(overlay);
            overlay.setData(info.drivingRouteLine);
            overlay.addToMap();
            overlay.zoomToSpan();
            MapStatusUpdate msu = MapStatusUpdateFactory.zoomTo(17.3f);
            mBaiduMap.setMapStatus(msu);
        }
    }

    private void initBMapView() {
        mBaiduMap = bmapView.getMap();
        MapStatusUpdate msu = MapStatusUpdateFactory.zoomTo(20.0f);
        mBaiduMap.setMapStatus(msu);
    }


    @Event(value = {R.id.back})
    private void viewOnClick(View v) {
        switch (v.getId()) {
            case R.id.back:
                this.finish();
                break;
//            case R.id.linear:
//                int isVis = recycler.getVisibility();
//                if(isVis == View.VISIBLE){
//                    //现在是显示 应该隐藏
////                    TranslateAnimation mHiddenAction = new TranslateAnimation(Animation.RELATIVE_TO_SELF,
////                            0.0f, Animation.RELATIVE_TO_SELF, 0.0f,
////                            Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF,
////                            -1.0f);
////                    mHiddenAction.setDuration(500);
////                    recycler.startAnimation(mHiddenAction);
//                    recycler.setVisibility(View.GONE);
//                    CommonUtil.setTextViewDrawableDirection(line_state,8,R.drawable.line_up);
//                }else{
//                    //显示
////                    TranslateAnimation mShowAction = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f,
////                            Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF,
////                            -1.0f, Animation.RELATIVE_TO_SELF, 0.0f);
////                    mShowAction.setDuration(500);
////                    recycler.startAnimation(mShowAction);
//                    recycler.setVisibility(View.VISIBLE);
//                    CommonUtil.setTextViewDrawableDirection(line_state,8,R.drawable.line_down);
//                }
//                break;
        }
    }

    // 定制RouteOverly
    private class MyDrivingRouteOverlay extends DrivingRouteOverlay {

        public MyDrivingRouteOverlay(BaiduMap baiduMap) {
            super(baiduMap);
        }

        @Override
        public BitmapDescriptor getStartMarker() {
            return BitmapDescriptorFactory.fromResource(R.drawable.icon_st);
        }

        @Override
        public BitmapDescriptor getTerminalMarker() {
            return BitmapDescriptorFactory.fromResource(R.drawable.icon_en);
        }
    }

    private class MyMassTransitRouteOverlay extends MassTransitRouteOverlay {

        public MyMassTransitRouteOverlay(BaiduMap baiduMap) {
            super(baiduMap);
        }

        @Override
        public BitmapDescriptor getStartMarker() {
            return BitmapDescriptorFactory.fromResource(R.drawable.icon_st);
        }

        @Override
        public BitmapDescriptor getTerminalMarker() {
            return BitmapDescriptorFactory.fromResource(R.drawable.icon_en);
        }
    }

    @Override
    public void initViewHandler() {
        LinearLayoutManager layoutVerManager = new LinearLayoutManager(QXApplication.getContext());
        layoutVerManager.setOrientation(LinearLayoutManager.VERTICAL);
        recycler.setLayoutManager(layoutVerManager);

        panel.setOnPanelListener(new Panel.OnPanelListener() {
            @Override
            public void onPanelClosed(Panel panel) {
                CommonUtil.setTextViewDrawableDirection(line_state, 8, R.drawable.line_up);
            }

            @Override
            public void onPanelOpened(Panel panel) {
                CommonUtil.setTextViewDrawableDirection(line_state, 8, R.drawable.line_down);
            }
        });
    }


    OnGetRoutePlanResultListener routeListener = new OnGetRoutePlanResultListener() {
        @Override
        public void onGetWalkingRouteResult(WalkingRouteResult walkingRouteResult) {

        }

        @Override
        public void onGetTransitRouteResult(TransitRouteResult transitRouteResult) {

        }

        @Override
        public void onGetMassTransitRouteResult(MassTransitRouteResult result) {
            //获取跨城综合公共交通线路规划结果
            List<MassTransitRouteLine> routeLines = result.getRouteLines();
            SystemPrintl.systemPrintl("我在执行绘制 公交路线----->>>>"+Thread.currentThread().getName());
            if (isRequestList(routeLines)) {
                MassTransitRouteLine line = routeLines.get(item);
                MassTransitRouteOverlay overlay = new MyMassTransitRouteOverlay(mBaiduMap);
                mBaiduMap.setOnMarkerClickListener(overlay);
                overlay.setData(line);
                overlay.addToMap();
                overlay.zoomToSpan();
                MapStatusUpdate msu = MapStatusUpdateFactory.zoomTo(17.3f);
                mBaiduMap.setMapStatus(msu);
            }

        }

        @Override
        public void onGetDrivingRouteResult(DrivingRouteResult drivingRouteResult) {

        }

        @Override
        public void onGetIndoorRouteResult(IndoorRouteResult indoorRouteResult) {

        }

        @Override
        public void onGetBikingRouteResult(BikingRouteResult bikingRouteResult) {

        }


    };

}
